package com.eig.orderweb.config;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.eig.orderweb.vo.AliPayBean;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * @author ：Q大侠
 * @date ：Created in 2023/1/6 0:39
 * @description：支付宝
 * @modified By：
 * @version:
 */
@Data
@Configuration
public class Alipay {

    /**日志对象*/
    private static final Logger logger = LoggerFactory.getLogger(com.eig.orderweb.config.Alipay.class);

    private final String format = "json";

    /**
     * appId
     */
    @Value("2021000122601821")
    private String appId;

    /**
     * 商户私钥
     */
    @Value("MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCAd6VDrswr4iWKmw3UXEXqT4Qv/Mm1WqU4veJJRiGvRqDVZUuAlGuWRzK+LFqlYjlePSmE81jupp1XOhz5o/EBDzxTrO2b3KvRdKikjmME/DDfUWDABiwdAkymTo6/zs5PRlVeZxg0a0wGKK1mh3yNxQk9mbEP5/mpjDtVDq2IdQHv5mnPxG0a+WroEEKQa3lUTfxWPQsZxRtdnG79hl62SYZiYGA+IegFMApw8r7UfQ2k6rz6CdyXQv5nMaUEVCfEL1rSZevtNPRG+4Yoep/byPuJJ82SUf9/lbG4luIliJlPy8dRrP4kcsFp/ZmnfSX6VlB15Hm8XpUHKTP0ziM1AgMBAAECggEANAQ9/nHAaeRfnwe5Scsy4/TeaS/J3IyfaaI5BcJH0ViftsgVEjMGfQrHGQmMinqDEA1jylleIQOFodP993bSFsO8gYT7kKiBSYYa4sZkgZg7kbC5pAbbxcMwlgYo9rEeCCtKVbcYreDkH3pI+8LQQ/cMDs00TGR5SpVlUMNosF9bWyHA9I40avhiqNjtreAwTCuCJ2+pR9OlXD9+z2nnyGhbFyWrvobdqdElxnt+8rzJWAJFC0C9eiZuvveJbEYfF8kMvVF60TYSvcgARpsry+6quFGN9LacPhkbDC+aE+dqMK66miMD5Wm4y/lBqVn85e0oilnUhd+siqRuy/KJ4QKBgQDVp0KL00MclhDam0ybQh2Ng4XmsSUAILKWxONYjRUyZImsNxTfnIrdEu7NtnCeA7lSpYabp3IYsx2rCC3KgPP8IkoKkXpf748h0qm73nisGYjSsbI3t5Oco6XAde+FDKKVv/bsqdPfzxpQJERpRa+I4xymKjvRNMu88w/UVDj3gwKBgQCZ7g+RhOMXApXZ/j1M8Rf9wYpvc66wD96aq6l32T4+R4+BQ8DJD57zocT7+skUaCbxNSZC/we+YjebtlH0QQG/wexex2G0j4GCnW3TGdCwd9O8uYzVKEOCYSwhXlXNltbAQvQAnnSTBcw6HLjTbzJs2u3kG1KJ0MZz9MFgZThE5wKBgQCCcAIdsZYYfrm9bPDBHQVD31dBd0b6o/vENmICLugRAvZWUK6KslzJCYUHzGlG30kmXu4mW305/SiQwYaLSl0qwphniVen+G5vHNkJXJJZp6Ez2M7QW2BgUjygmp/nn8YnHLKY3DoBOsR6xxmJF8VKEjXscH5ihBQawF4aqGy7FwKBgAI70SAEdKMl13TVWqBeM0HsaAzb+eH2KP3Hler2TQ/sS+nNSOPiBSV16Hzbk8VnCYGgitCYEM2zcSkiSSgpNVRKBRsO+5z6JB/4FtrkWgHliz4G10NUlT40GMbXLpmQIceicRX9NqXXhLInLotq5qlrt94Cg30aeXa/S3sRnPJrAoGAfCiP16jEGh+DhJcVOBGGtKwEAKHk+wIWs41sn5yqZgHKYxn3yusDfkVP5izf3ZxzIUUfr2u5XBjTNL/y9TKYm7qTI3guBdWW36qIRVFzdvJTbdkoaI/7zkoAx7YNNgPHzgoBbITAuhzZtLOB3cKP4pUPnJ/jyCx/W2e/Vsvkcig=")
    private String privateKey;

    /**
     * 支付宝公钥
     */
    @Value("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsx4XDjY16LNYJbeVTO2mG+5s9V3FHucrnnbOMROShHybXMYes0RIlA0Y/IFQecsQCNfft8FjWmEfUQZdQcELDlluqxWHQjOm3ILUyVXloAJ1pSMpQkhUY8LkI1lhQ3ZsPzsCLQxVPpE/yyG5lSzSTxXzDkAIqK6DuCYPNMFQPiNqC1uY2oshSNsq5QN0BZosLAGsasUKRueQ0+fIJAuNm5jeoz5CEEYaSafv1ger9YOeXPMFDB8CaaU0U6/vH65f25ZMpYkPmJAYmNfB3M6a/LK1K9DBJ70rAvRZvCwpKXfMg3HbXv0l2WCKYMCpiU79FtrYeb35CE4jUBpzWFqhJwIDAQAB")
    private String publicKey;

    /**
     * 服务器异步通知页面路径，需要公网能访问到
     */
    @Value("http://localhost:8081/DetailId/")
    private String notifyUrl;

    /**
     * 服务器同步通知页面路径，填写自己的成功页面路径
     */
    @Value("http://localhost:8081/DetailId")
    private String returnUrl;

    /**
     * 签名方式
     */
    @Value("RSA2")
    private String signType;

    /**
     * 字符编码格式
     */
    @Value("utf-8")
    private String charset;

    /**
     * 支付宝网关
     */
    @Value("https://openapi.alipaydev.com/gateway.do")
    private String gatewayUrl;

    public String pay(String courseId, AliPayBean aliPayBean) throws AlipayApiException {

        AlipayClient alipayClient = new DefaultAlipayClient(
                gatewayUrl, appId, privateKey, format, charset, publicKey, signType);

        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(returnUrl+courseId);
        alipayRequest.setNotifyUrl(notifyUrl+courseId);
        alipayRequest.setBizContent(JSON.toJSONString(aliPayBean));
        logger.info("封装请求支付宝付款参数为:{}", JSON.toJSONString(alipayRequest));

        String result = alipayClient.pageExecute(alipayRequest).getBody();
        logger.info("请求支付宝付款返回参数为:{}", result);

        return result;
    }
}


